<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
    margin: 0;
    padding: 0;
}
.tab{
    width: 617px;
    margin: 100px auto 0;
}
.options{
    overflow: hidden;
    border-top: 2px solid #06A86E;
}
.options h3{
    float: left;
    width: 150px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    background-color: #F7F8F8;
    font-size: 18px;
    font-weight: normal;
    color: #666;
    cursor: pointer;
}
.contents{
    height: 200px;
    padding: 10px 0;
    /* overflow: hidden; */
    overflow: auto;
}
.contents div{
    overflow: hidden;
    margin-bottom: 10px;
    /* display: none; */
}
.contents div img{
    width: 150px;
    height: 200px;
    float: left;
}
/* 选中状态的样式 */
.options h3.active{
    color: #fff;
    background-color: #06A86E;
}
.contents div.show{
    display: block;
}
</style>
</head>
<body>
    
<!-- 最外层容器 -->
<div class="tab">
    <!-- 选项区 -->
    <div class="options">
        <h3 class="active">科幻</h3>
        <h3>爱情</h3>
        <h3>动作</h3>
        <h3>喜剧</h3>
    </div>
    <!-- 内容区 -->
    <div class="contents">
        <div class="show">
            <img src="./imgs/e1.png" alt="">
            <img src="./imgs/e1.png" alt="">
            <img src="./imgs/e1.png" alt="">
            <img src="./imgs/e1.png" alt="">
        </div>
        <div>
            <img src="./imgs/e2.png" alt="">
            <img src="./imgs/e2.png" alt="">
            <img src="./imgs/e2.png" alt="">
            <img src="./imgs/e2.png" alt="">
        </div>
        <div>
            <img src="./imgs/e3.png" alt="">
            <img src="./imgs/e3.png" alt="">
            <img src="./imgs/e3.png" alt="">
            <img src="./imgs/e3.png" alt="">
        </div>
        <div>
            <img src="./imgs/e4.png" alt="">
            <img src="./imgs/e4.png" alt="">
            <img src="./imgs/e4.png" alt="">
            <img src="./imgs/e4.png" alt="">
        </div>
    </div>
</div>

<script>
// 获取元素
var h3s = document.querySelectorAll('.options h3');
var contents = document.querySelector('.contents');

var prevIndex = 0;// 记录上次选中项的索引

// 在JS中，只要内容大于容器，就可以操作容器的滚动条，无论样式设置是否显示滚动条
// contents的scrollTop和当前选项的索引相关：
// contents.scrollTop = 当前选项的索引*contents.clientHeight
// console.log( contents.clientHeight );

// 给每一个选项添加点击事件
for (var i = 0; i < h3s.length; i++) {
    // 给每一选项添加index属性，记录自己的索引
    h3s[i].index = i;
    h3s[i].onclick = function (){// 事件处理函数
        // this -> 点击的那个元素
        // this.index -> 点击选项的索引

        // 去掉上次选中项的类名
        h3s[prevIndex].className = '';
        // 当前选项添加类名
        this.className = 'active';
        // 更新上次选中项的索引
        prevIndex = this.index;

        // 设置contents的滚动条
        contents.scrollTop = this.index * (contents.clientHeight-10);
    }
}

</script>
</body>
</html>